﻿@using System.Linq;
@using PortalIntranet.InternalComponents.Common;
@using PortalIntranet.InternalComponents.Configuration;
@using PortalIntranet.InternalComponents.Security;
@using PortalIntranet.DatabaseComponents;
@using PortalIntranet.ObjectComponents.Administracao;
@using PortalIntranet.ObjectComponents.Comun;
@using PortalIntranet.ObjectComponents.WebParts;

@model IntranetApplication.Models.Dynamic.DynamicModel

@{
	ViewBag.Title = Model.Nome;
	Layout = "~/Views/Shared/_Layout.cshtml";
	bool exibirMenus = (Model.WebPages != null && Model.WebPages.Count > 1);
}

@section WebPartStyles{
	<link rel="stylesheet" href="@Url.Content("~/Content/Pages/dynamic.css")">
	<link rel="stylesheet" href="@Url.Content("~/Content/WebParts/galerias.css")">
}

<!-- breadcrumbs -->
<div class="container">
	<ul id="breadcrumbs">
		<li><a href="@AppSettings.UrlHome"><i class="icon-home"></i></a></li>
		@{
			string[] crumbs = Model.Uri.BreadCrumbs.Split('/');
			string[] urls = Model.Uri.UrlAddress.Split('/');
			CustomPrincipal principal = HttpContext.Current.User as CustomPrincipal;
			MenuItemInfo menu = null;
			string link = string.Empty;
			
			if (principal != null)
			{
				for (int i = 0; i < crumbs.Length - 1; i++)
				{
					if (menu == null)
					{
						menu = principal.Menus.Where(c => c.Slug.ToLower() == urls[i].ToLower()).SingleOrDefault();
					}
					else
					{
						menu = menu.SubMenus.Where(c => c.Slug.ToLower() == urls[i].ToLower()).SingleOrDefault();	
					}
					
					link = (menu == null ? Model.Uri.UrlAddress : menu.Uri.UrlAddress);					
					<li><a href="@(string.IsNullOrWhiteSpace(link) ? "javascript:void(0)" : string.Format("{0}/{1}", AppSettings.UrlBase, link))">@crumbs[i]</a></li>
				}
			}
		}

		<li><span>@Model.Nome</span></li>
	</ul>
</div>

<!-- main content -->
<div class="container">
	<div class="row-fluid">
		@{
			if (exibirMenus)
			{
				<div class="span2">
					<div class="sidebar">
						<ul id="pageNav">
							@{
								foreach (var pg in Model.WebPages)
								{
									<li class="@(pg.Atual ? "current" : "")"><a class="jnav-ext" href="@Url.Action("Index", "Dynamic", new { wpg = @pg.Slug })">@pg.Nome</a></li>
								}
							 }
						</ul>
					</div>
				</div>
			}
		}
		<div id="portlet_container" class="@(exibirMenus ? "span10" : "span12")">
			@{	   
				List<PortletInfo> superior = Model.Portlets.Where(c => c.Linha == LinhaPortlet.Superior).ToList();
				List<PortletInfo> inferior = Model.Portlets.Where(c => c.Linha == LinhaPortlet.Inferior).ToList();
                List<WebPartBaseInfo> webparts = null;
                int k = 0;
				
				if (superior != null && superior.Count > 0)
				{
					<div class="row-fluid" style="margin-top: 0px !important;">
					@{
						foreach (var ptx in superior)
						{
							<ol id="@string.Format("Portlet-0-{0}", k.ToString())" class="portlet column @Common.GetDescription(ptx.Largura)">
                                @{
									webparts = Model.WebParts.Where(c => c.Linha == LinhaPortlet.Superior && c.Coluna == k).ToList();
									
                                    foreach (var wpx in webparts)
                                    {
                                        <li id="@string.Format("WebPart-{0}", wpx.Id.ToString())">
											@Html.Partial(AppSettings.PartialViewBase, wpx)
                                        </li>
                                    }
                                }
							</ol>
                            k++;
						}
					}
					</div>
				}

				if (inferior != null && inferior.Count > 0)
				{
					<div class="row-fluid" style="margin-top: 0px !important;">
					@{
                        k = 0;
                        
						foreach (var pty in inferior)
						{
							<ol id="@string.Format("Portlet-1-{0}", k.ToString())" class="portlet column @Common.GetDescription(pty.Largura)">
                                @{
                                    webparts = Model.WebParts.Where(c => c.Linha == LinhaPortlet.Inferior && c.Coluna == k).ToList();
									
                                    foreach (var wpy in webparts)
                                    {
                                        <li id="@string.Format("WebPart-{0}", wpy.Id.ToString())">
											@Html.Partial(AppSettings.PartialViewBase, wpy)
                                        </li>
                                    }
                                }
							</ol>
                            k++;
						}
					}
					</div>
				}
			}
		</div>
	</div>
</div>

@section WebPartScripts{
	<script src="@Url.Content("~/Scripts/Pages/dynamic.js")"></script>
	<script src="@Url.Content("~/Scripts/WebParts/galerias.js")"></script>
	<script src="@Url.Content("~/Scripts/WebParts/webparts.js")"></script>
}
